package com.newlink.building.record.fragment

import android.content.Intent
import android.os.Looper
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.newlink.building.common_base.activities.Base_ImageZoomActivityNL
import com.newlink.building.common_base.base.BaseMvpFragment
import com.newlink.building.common_base.ext.setNewOrAddData
import com.newlink.building.common_base.model.bean.OpenDoorRecordListData
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.WeakHandler
import com.newlink.building.record.R
import com.newlink.building.record.adapter.Module_Record_OpenDoorRecordAdapter
import com.newlink.building.record.databinding.FragmentOpenDoorRecordBinding
import com.newlink.building.record.mvp.Module_Record_OpenDoorRecordPresenter
import com.newlink.building.record.mvp.contract.Module_Record_OpenDoorRecordContract
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker

/**
 * @author wangyang
 * @date 2022/9/23
 * @description 记录——开门记录页面
 */
class Module_Record_OpenDoorRecordFragment :
    BaseMvpFragment<Module_Record_OpenDoorRecordContract.View, Module_Record_OpenDoorRecordContract.Presenter>(),
    Module_Record_OpenDoorRecordContract.View {

    private val weakHandler by lazy { WeakHandler(Looper.getMainLooper()) }
    private var curList: MutableList<OpenDoorRecordListData> = ArrayList()
    private var curPage: Int = 1
    private var curLimit: Int = 20
    private var mAdapter: Module_Record_OpenDoorRecordAdapter? = null
    private var isRefreshLayoutRequest: Boolean = false
    private var empty_content: View? = null
//    override fun attachLayoutRes(): Int {
//        return R.layout.fragment_open_door_record
//    }

    private lateinit var mBinding: FragmentOpenDoorRecordBinding
    override fun attachLayoutRes(): View {
        mBinding = FragmentOpenDoorRecordBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView(view: View) {
        // 开门记录页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("OpenDoorRecordFragment", "initView")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("door_record")

        empty_content = view.findViewById(R.id.empty_content)
        mBinding.swipeRefreshLayout.setOnRefreshListener {
            isRefreshLayoutRequest = true
            weakHandler.postDelayed({
                curPage = 1
                loadRecordData()
            }, 500)
        }
        mAdapter = Module_Record_OpenDoorRecordAdapter(curList)
        mBinding.rvOpenDoorRecord.adapter = mAdapter

        mAdapter?.run {
            setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn)
            loadMoreModule.setOnLoadMoreListener {
                isRefreshLayoutRequest = false
                ++curPage
                showLoading()
                weakHandler.postDelayed({ loadRecordData() }, 500)
            }
            setOnItemChildClickListener { adapter, view, position ->
                Intent(requireContext(), Base_ImageZoomActivityNL::class.java).apply {
                    putExtra("image_url", curList[position]?.fileUrl)
                    startActivity(this)
                }
            }
//            setOnItemClickListener { _, _, position ->
//                val intent = Intent(context, PreviewDetailActivityty::class.java)
//                intent.putExtra("record_data",curList[position])
//                startActivity(intent)
//            }
        }
    }

    override fun onResume() {
        super.onResume()
        curPage = 1
        loadRecordData()
    }

    override fun lazyLoadDataIfPrepared() {
    }

    override fun lazyLoad() {
        NLog.e("OpenDoorRecordFragment lazyLoad >>  page : $curPage limit: $curLimit ")
        loadRecordData()
    }

    private fun loadRecordData() {
        // 加载开门记录时的混淆
        Module_Obfuscation_ObfuscationHelper.beforeDatabaseOperation("LOAD_DOOR_RECORDS", curPage.toString())
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(17, curPage)

        mPresenter?.getOpenDoorRecordList(accessToken, curPage, curLimit)
    }

    override fun showLoading() {
        mBinding.swipeRefreshLayout.isRefreshing = true
    }

    override fun hideLoading() {
        isRefreshLayoutRequest = false
        mBinding.swipeRefreshLayout.isRefreshing = false
    }

    override fun createPresenter(): Module_Record_OpenDoorRecordContract.Presenter = Module_Record_OpenDoorRecordPresenter()

    override fun getOpenDoorRecordListSuccess(
        total: Int,
        list: MutableList<OpenDoorRecordListData>?
    ) {
        if (isRefreshLayoutRequest) {
//            showToast(getString(R.string.default_list_data_refresh))
            hideLoading()
            curList?.clear()
        }

        list?.let {
            if (curPage == 1) {//加载第一页
                NLog.e("is first page,set new data")
                curList.clear()
                curList = it
                mAdapter?.setNewOrAddData(true, list)
            } else {//不是第一页,则添加数据
                NLog.e("current page is $curPage,11111 = ${curList.size} it.size = ${it.size}")
                curList.addAll(it)
                NLog.e("current page is $curPage,22222 = ${curList.size}")
                mAdapter?.setNewOrAddData(false, list)
            }
        }
        NLog.e("display size:${curList.size} total:$total pageNum:$curPage ")
        //小于总条数,继续加载更多,否则停止加载更多
        if (curList.size < total) {
            mAdapter?.loadMoreModule?.loadMoreComplete()
        } else {
            mAdapter?.loadMoreModule?.loadMoreEnd()
        }

        //是否加载空页面
        if (curList.size == 0) {
            empty_content?.visibility = View.VISIBLE
            mBinding.rvOpenDoorRecord.visibility = View.GONE
        } else {
            empty_content?.visibility = View.GONE
            mBinding.rvOpenDoorRecord.visibility = View.VISIBLE
        }
    }

    override fun getOpenDoorRecordListFail() {
        hideLoading()
    }
}